//index.js
//获取应用实例
const tabService = require("../../utils/tab-service")

import {
  Apilogin,
  ApiCaptchaCode,
  ApiCaptchaCodeLogin
} from "../../api/index"
const app = getApp()
let username = '13164640637'
let password = '123456'
let code = ''
Page({
  data: {
    username: '13164640637',
    password: '123456',
    code: '',
    clientHeight: '',
    countdown: 0,
    active: false,
    navBarHeight: app.globalData.navBarHeight,
    menuRight: app.globalData.menuRight,
    menuBotton: app.globalData.menuBotton,
    menuTop: app.globalData.menuTop,
    menuHeight: app.globalData.menuHeight,
    menuWidth: app.globalData.menuWidth,
  },
  onLoad() {
    var that = this
    wx.getSystemInfo({
      success: function (res) {
        that.setData({
          clientHeight: res.windowHeight
        });
      }
    })
  },

  returnIndex() {
    wx.reLaunch({
      url: '/pages/servicelIst/index',
    })
  },
  onChange(e) {
    this.setData({
      active: !this.data.active
    })
  },
  onLogin() {
    wx.clearStorage()
    let that = this
    this.setData({
      loading: true
    })
    if (this.data.active) {
      wx.login({
        success: (res) => {
          Apilogin({
            code: res.code
          }).then(res => {
            app.setToken(res.data.token, res.data)
            wx.setStorageSync('loginType', "patient")
            wx.switchTab({
              url: '/pages/servicelIst/index'
            })
            /*    wx.reLaunch({
                 url: '/pages/home/index',
               }) */
          })


        },
        fail: (err) => {
          wx.showToast({
            title: '获取code失败',
            icon: "none"
          })
        }
      })
    } else {
      /*   wx.showToast({
        title: "请勾选用户协议和隐私政策",
        icon: 'error'
      })
 */
      wx.showModal({
        title: "请阅读并勾选《用户协议和隐私政策》！",
        icon: 'error',
        showCancel: false
      })
      this.setData({
        loading: false
      })
    }

  },

  //获取优化
  privacyAgreement() {
    wx.navigateTo({
      url: '/components/privacyAgreement/index',
    })
  },


  //护理人员登录
  goDoctor(e) {
    this.doubleTap(e)


  },
  touchStart: function (e) {
    console.log(e)
    this.touchStartTime = e.timeStamp;
  },

  // 按钮触摸结束触发的事件
  touchEnd(e) {
    this.touchEndTime = e.timeStamp;
  },

  /// 双击
  doubleTap(e) {
    console.log(e)
    const that = this;
    // 控制点击事件在350ms内触发，加这层判断是为了防止长按时会触发点击事件
    if (that.touchEndTime - that.touchStartTime < 350) {
      // 当前点击的时间
      var currentTime = e.timeStamp
      var lastTapTime = that.lastTapTime
      console.log(currentTime);
      console.log(lastTapTime)
      // 更新最后一次点击时间
      that.lastTapTime = currentTime
      // 如果两次点击时间在300毫秒内，则认为是双击事件
      if (currentTime - lastTapTime < 300) {
        console.log("double tap")
        // 成功触发双击事件时，取消单击事件的执行
        clearTimeout(that.lastTapTimeoutFunc);
        wx.navigateTo({
          url: '/pagesDoctor/doctorLogin/index',
        })
      };
    };
  },
  //获取输入款内容
  content(e) {
    username = e.detail
  },
  password(e) {
    password = e.detail
  },
  code(e) {
    code = e.detail
  },
  //登录事件
  goadmin(val) {
    if (val.currentTarget.dataset.v == 'code') {
      this.getCodeLogin()
    } else {
      this.getPassword()
    }
  },
  //验证码登录
  getCodeLogin() {
    let query = {
      phone: username,
      code: code,
    }
    ApiCaptchaCodeLogin(query).then(res => {
      app.setToken(res.data.token, res.data)
      wx.reLaunch({
        url: '/pages/servicelIst/index',
      })
    })
  },
  //密码登录
  getPassword() {
    let query = {
      phone: username,
      password: password,
    }
    Apilogin(query).then(res => {
      app.setToken(res.data.token, res.data)
      wx.reLaunch({
        url: '/pages/servicelIst/index',
      })
    })
  },
  //获取验证码
  getCaptchaCode() {
    ApiCaptchaCode({
      phone: username,
    }).then(res => {
      this.setData({
        countdown: 60,
      }, () => {
        this.getCountdown()
      })
    })
  },
  getCountdown() {
    this.timer = setInterval(_ => {
      const countdown = this.data.countdown - 1
      if (countdown < 1) {
        clearInterval(this.timer)
        this.timer = null
      }
      this.setData({
        countdown
      })
    }, 1000)
  },
  //tab切换
  onChangeTab(e) {
    this.setData({
      tabLabel: e.detail.name
    })
    username = ''
    password = ''
    code = ''
    this.setData({
      username: '',
      password: '',
      code: '',
    })
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})